using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content.Pipeline;
using Microsoft.Xna.Framework.Content.Pipeline.Graphics;
using Microsoft.Xna.Framework.Content.Pipeline.Processors;
using Microsoft.Xna.Framework.Content.Pipeline.Serialization.Compiler;

namespace CrateManiaPipeline
{
    /// <summary>
    /// This class will be instantiated by the XNA Framework Content Pipeline
    /// to write the specified data type into binary .xnb format.
    ///
    /// This should be part of a Content Pipeline Extension Library project.
    /// </summary>
    [ContentTypeWriter]
    public class TileMapWriter : ContentTypeWriter<TileMapContent>
    {
        protected override void Write(ContentWriter output, TileMapContent value)
        {
            output.Write(value.Width);
            output.Write(value.Height);
            if (value.Tileset != null)
            {
                output.Write(false);
                output.WriteObject(value.Tileset, new TilesetWriter());
            }
            if (value.TilesetRef != null)
            {
                output.Write(true);
                output.WriteExternalReference<TilesetContent>(value.TilesetRef);
            }
            output.Write(value.Layers.Count);
            foreach (TileMapLayerContent layer in value.Layers)
                Write(output, layer);
        }

        void Write(ContentWriter output, TileMapLayerContent value)
        {
            output.Write(value.Name);
            output.Write(value.Width);
            output.Write(value.Height);
            output.Write(value.IsVisible);
            output.WriteObject(value.Tiles,new IntIntPairListWriter());
        }

        public override string GetRuntimeReader(TargetPlatform targetPlatform)
        {
            return "JumpNFight.Simulation.TileMapReader, JumpNFight";
        }
    }
}
